<?php

namespace App\Http\Controllers\Admin\Config;

use App\Http\Requests\Admin\Config\GameRequest;
use App\Models\Config\Games;
use App\Services\Config\GamesService;

use App\Http\Controllers\Admin\BaseController;
use Illuminate\Http\Request;

class GamesController extends BaseController
{

    public function __construct(GamesService $gamesService)
    {
        $this->gamesService = $gamesService;
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        $reqPageSize = $request->page_size;
        $games = $this->gamesService->getPageGames($request);

        return view('config.games.index', compact('games', 'reqPageSize'));
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create()
    {
        return view('config.games.create');
    }

    public function store(GameRequest $request)
    {
        Games::create($request->all());

        flash('添加游戏成功')->success()->important();

        return $this->redirect($request);
    }


    /**
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit($id)
    {
        $game = $this->gamesService->ById($id);

        return view('config.games.edit', compact('game'));
    }

    public function update(GameRequest $request, $id)
    {
        $this->gamesService->update($request, $id);

        flash('更新游戏成功')->success()->important();

        return $this->redirect($request);
    }

    /**
     * @param $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function delete($id)
    {
        $game = $this->gamesService->ById($id);

        if (empty($game)) {
            flash('删除失败')->error()->important();

            return redirect()->back();
        }

        $game->delete();

        flash('删除成功')->success()->important();

        return redirect()->back();
    }

    /**
     * @param $status
     * @param $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function status($status, $id)
    {
        $game = $this->gamesService->ById($id);

        if (empty($game)) {
            flash('操作失败')->error()->important();
            return redirect()->back();
        }

        $game->update(['status' => $status]);

        flash('更新状态成功')->success()->important();

        return redirect()->back();
    }

}
